package HashMap;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

/**
 * Date: 2023-04-19
 * Time: 16:10
 */
public class HashDemo1 {
    public static void main(String[] args) {
        HashMap<Student,String> hashMap = new HashMap<>();
        Student s1 = new Student("zhangsan",18);
        Student s2 = new Student("lisi",18);
        Student s3 = new Student("wangwu",18);

        hashMap.put(s1,"上海");
        hashMap.put(s2,"北京");
        hashMap.put(s3,"深圳");


        // 遍历集合
        // 获取所有的键,存储在单列集合中
        System.out.println("----------------------- 键找值-------------------");
        Set<Student> students = hashMap.keySet();
        for(Student key:students){
            String value = hashMap.get(key);
            System.out.println(key+"="+value);
        }

        //键值对
        System.out.println("---------------------键值对----------------------");

        // 获取双列集合
        Set<Map.Entry<Student, String>> entries = hashMap.entrySet();

        //Iterator<Map.Entry<Student,String>> it2 = hashMap.iterator();

        // 获取迭代器对象
        Iterator<Map.Entry<Student, String>> it = entries.iterator();

        // 循环遍历
        while(it.hasNext()){
            Map.Entry<Student, String> key = it.next();
            String value = hashMap.get(key);
            System.out.println(key+"="+value);
        }

        // Lambda 表达式
        System.out.println("--------------------------- Lambda ----------------------------");
        hashMap.forEach((key,value)-> System.out.println(key+"="+value));


        // 匿名内部类
        hashMap.forEach(new BiConsumer<Student, String>() {
            @Override
            public void accept(Student student, String s) {
                System.out.println(student+"="+s);
            }
        });
    }
}
